﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chinaer.Core.Helper
{
    public class GenerateIDHelper
    {
        /// <summary>
        /// 根据时间生成对应的键值
        /// </summary>
        /// <returns></returns>
        public static string GenerateID()
        {
            DateTime dtNow = DateTime.Now;
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("{0:D4}",dtNow.Year));
            sb.Append(string.Format("{0:D2}",dtNow.Month));
            sb.Append(string.Format("{0:D2}",dtNow.Day));
            sb.Append(string.Format("{0:D2}",dtNow.Hour));
            sb.Append(string.Format("{0:D2}",dtNow.Minute));
            sb.Append(string.Format("{0:D2}",dtNow.Second));
            sb.Append(string.Format("{0:D3}",dtNow.Millisecond));
            sb.Append(GenerateRandom());
            return sb.ToString();
        }
        /// <summary>
        /// 生成指定位数的字符串
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private static string GenerateRandom(int count = 15)
        {
            if (count > 32)
            {
                throw new Exception("无法生成指定的位数：" + count.ToString() + "太大");
            }
            return Guid.NewGuid().ToString().Replace("-", "").Substring(0, count);
        }
    }
}
